import 'dart:convert';

/// 练习任务模型
class PracticeTaskModel {
  String id;
  String title;
  bool isCompleted;
  DateTime? createdAt;
  DateTime? completedAt;

  PracticeTaskModel({
    required this.id,
    required this.title,
    this.isCompleted = false,
    this.createdAt,
    this.completedAt,
  }) {
    createdAt ??= DateTime.now();
  }

  /// 从 JSON Map 创建实例
  factory PracticeTaskModel.fromJson(Map<String, dynamic> json) {
    return PracticeTaskModel(
      id: json['id'] as String,
      title: json['title'] as String,
      isCompleted: json['isCompleted'] as bool? ?? false,
      createdAt: json['createdAt'] != null 
          ? DateTime.fromMillisecondsSinceEpoch(json['createdAt'] as int)
          : null,
      completedAt: json['completedAt'] != null 
          ? DateTime.fromMillisecondsSinceEpoch(json['completedAt'] as int)
          : null,
    );
  }

  /// 从 JSON 字符串创建实例
  factory PracticeTaskModel.fromJsonString(String jsonString) {
    final json = jsonDecode(jsonString) as Map<String, dynamic>;
    return PracticeTaskModel.fromJson(json);
  }

  /// 转换为 JSON Map
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'isCompleted': isCompleted,
      'createdAt': createdAt?.millisecondsSinceEpoch,
      'completedAt': completedAt?.millisecondsSinceEpoch,
    };
  }

  /// 转换为 JSON 字符串
  String toJsonString() {
    return jsonEncode(toJson());
  }

  /// 标记任务为完成
  void markAsCompleted() {
    isCompleted = true;
    completedAt = DateTime.now();
  }

  /// 标记任务为未完成
  void markAsIncomplete() {
    isCompleted = false;
    completedAt = null;
  }

  /// 切换完成状态
  void toggleCompletion() {
    if (isCompleted) {
      markAsIncomplete();
    } else {
      markAsCompleted();
    }
  }

  /// 是否为空任务
  bool get isEmpty => title.trim().isEmpty;

  /// 是否为有效任务
  bool get isValid => title.trim().isNotEmpty;

  /// 获取任务持续时间（如果已完成）
  Duration? get duration {
    if (completedAt != null && createdAt != null) {
      return completedAt!.difference(createdAt!);
    }
    return null;
  }

  /// 复制实例
  PracticeTaskModel copyWith({
    String? id,
    String? title,
    bool? isCompleted,
    DateTime? createdAt,
    DateTime? completedAt,
  }) {
    return PracticeTaskModel(
      id: id ?? this.id,
      title: title ?? this.title,
      isCompleted: isCompleted ?? this.isCompleted,
      createdAt: createdAt ?? this.createdAt,
      completedAt: completedAt ?? this.completedAt,
    );
  }

  @override
  String toString() {
    return 'PracticeTaskModel(id: $id, title: $title, isCompleted: $isCompleted, createdAt: $createdAt, completedAt: $completedAt)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is PracticeTaskModel &&
        other.id == id &&
        other.title == title &&
        other.isCompleted == isCompleted &&
        other.createdAt == createdAt &&
        other.completedAt == completedAt;
  }

  @override
  int get hashCode {
    return Object.hash(id, title, isCompleted, createdAt, completedAt);
  }
}
